/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function (a, b) {
  const maxLength = Math.max(a.length, b.length);
  a = a.split("").reverse().join("");
  b = b.split("").reverse().join("");
  let carry = 0;

  const result = [];

  for (let i = 0; i < maxLength; i++) {
    let temNum = (Number(a[i]) || 0) + (Number(b[i]) || 0) + carry;
    if (temNum == 2) {
      carry = 1;
      result.push(0);
    } else if (temNum == 3) {
      carry = 1;
      result.push(1);
    } else {
      carry = 0;
      result.push(temNum);
    }
  }

  if (carry) result.push(1);

  return result.reverse().join("");
};

console.log(addBinary("1111", "1111"));
